package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Json交互
 * @author lixu
 */
@RestController
//@Controller
@RequestMapping("/json")
//@ResponseBody  //整个类返回值都是要将对象转成JSON串的
public class JsonController {


   // private ObjectMapper objectMapper = new ObjectMapper();//jackson包
    /**
     * 第一：
     *
     * 页面发送 JSON串：  {id:123,name:xixi}
     *
     * SpringMvc:

     * @RequestBody :
     *              默认是使用 jackson 的JSON转换
     *      *      User user1 = objectMapper.readValue("{id:123,name:xixi}", User.class);
     *       ----------------------------------------------------------------------
     *              // 阿里巴巴 JSOn转换包
     *      *      User user2 = JSONObject.parseObject("{id:123,name:xixi}", User.class);
     * @ResponseBody
     *             String json = objectMapper.writeValueAsString(user);
     *             String json2 = JSONObject.toJSONString(user);
     */
    @PostMapping("/test1")
    public User test1(@RequestBody User user) throws Exception{
        //添加  修改   删除  查询 CRUD
        //返回值JSON串
        user.setId(456);
        user.setName("刘德华");
        return user;
    }





}
